//package com.jbt.mds.obdsdk
//
//import android.os.Bundle
//import android.text.Editable
//import android.text.TextWatcher
//import android.util.Log
//import android.widget.Button
//import android.widget.EditText
//import androidx.appcompat.app.AlertDialog
//import androidx.appcompat.app.AppCompatActivity
//import androidx.recyclerview.widget.LinearLayoutManager
//import androidx.recyclerview.widget.RecyclerView
//import com.jbt.mds.obdsdk.adapter.VehicleHistoryAdapter
//import com.jbt.mds.obdsdk.extension.SPUtils
//import com.jbt.mds.obdsdk.model.VehicleHistory
//import okhttp3.*
//import org.json.JSONObject
//import java.io.IOException
//import java.util.concurrent.TimeUnit
//
//class DqlsActivity : AppCompatActivity() {
//    private lateinit var adapter: VehicleHistoryAdapter
//    private var allList: List<VehicleHistory> = emptyList()
//
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_dqls)
//
//        val recyclerView = findViewById<RecyclerView>(R.id.rv_history)
//        val etSearch = findViewById<EditText>(R.id.et_search)
//        val btnSearch = findViewById<Button>(R.id.btn_search)
//
//        // 3. 初始化Adapter时传入一个回调函数
//        adapter = VehicleHistoryAdapter(emptyList()) { jsonString ->
//            showFailReasonDialog(jsonString)
//        }
//        recyclerView.layoutManager = LinearLayoutManager(this)
//        recyclerView.adapter = adapter
//
//        btnSearch.setOnClickListener {
//            filterList(etSearch.text.toString())
//        }
//        etSearch.addTextChangedListener(object : TextWatcher {
//            override fun afterTextChanged(s: Editable?) {
//                filterList(s.toString())
//            }
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
//        })
//
//        qy()
//    }
//
//    // 4. 新增弹窗方法
//    private fun showFailReasonDialog(jsonString: String) {
//        val details = StringBuilder()
//        try {
//            val jsonObject = JSONObject(jsonString)
//            val keys = jsonObject.keys()
//            var hasFailure = false
//            while (keys.hasNext()) {
//                val key = keys.next()
//                val itemObject = jsonObject.getJSONObject(key)
//                if (itemObject.optInt("code") == 0) { // 假设 code 0 代表不合格
//                    details.append("• ${itemObject.optString("msg")}\n\n")
//                    hasFailure = true
//                }
//            }
//            if (!hasFailure) {
//                details.append("无具体不合格项信息。")
//            }
//        } catch (e: Exception) {
//            details.append("无法解析不合格项详情。")
//            e.printStackTrace()
//        }
//
//        AlertDialog.Builder(this)
//            .setTitle("不合格项详情2")
//            .setMessage(details.toString())
//            .setPositiveButton("确定", null)
//            .show()
//    }
//
//    private fun filterList(keyword: String) {
//        val filtered = if (keyword.isBlank()) {
//            allList
//        } else {
//            allList.filter {
//                it.plate.contains(keyword, true) ||
//                        it.vin.contains(keyword, true) ||
//                        it.vehicleType.contains(keyword, true) ||
//                        it.status.contains(keyword, true)
//            }
//        }
//        adapter.updateData(filtered)
//    }
//
//    private fun qy() {
//        val client = OkHttpClient.Builder()
//            .callTimeout(1, TimeUnit.DAYS)
//            .readTimeout(1, TimeUnit.DAYS)
//            .writeTimeout(1, TimeUnit.DAYS)
//            .build()
//
//        val request = Request.Builder()
//            .url("http://115.153.96.26:10061/calculation-results?page=1&pageSize=10")
//            .get()
//            .addHeader("Authorization", "Bearer " + SPUtils.getString("token", ""))
//            .build()
//
//        client.newCall(request).enqueue(object : Callback {
//            override fun onFailure(call: Call, e: IOException) {
//                // Handle failure
//            }
//
//            override fun onResponse(call: Call, response: Response) {
//                val responseBody = response.body?.string() ?: return
//                val json = JSONObject(responseBody)
//                Log.i("11110", json.toString())
//                val data = json.optJSONObject("data")
//                val rows = data?.optJSONArray("rows") ?: return
//                val list = ArrayList<VehicleHistory>()
//                for (i in 0 until rows.length()) {
//                    val obj = rows.getJSONObject(i)
//                    val plate = obj.optString("license")
//                    val vin = obj.optString("vin")
//                    val time = obj.optString("createTime").replace("T", " ").replace("Z", "")
//                    val vehicleType = obj.optString("type")
//                    val status = when (obj.optInt("resultStatus")) {
//                        0 -> "不合格"
//                        1 -> "合格"
//                        2 -> "疑似篡改"
//                        else -> "未知状态" // 处理 resultStatus 为其他值的情况，这是一个好的实践
//                    }
//                    // 2. 获取 executeResult
//                    val executeResult = obj.optString("executeResult", "{}")
//                    list.add(VehicleHistory(plate, vin, time, vehicleType, status, executeResult))
//                }
//                runOnUiThread {
//                    allList = list
//                    adapter.updateData(list)
//                }
//            }
//        })
//    }
//}
